﻿namespace breakoutsharp
{
	using System;
	using System.Collections.Generic;
	using System.ComponentModel;
	using System.Data;
	using System.Drawing;
	using System.Linq;
	using System.Text;
	using System.Windows.Forms;
	using breakoutsharp.visual.gameobject;

	public partial class LevelEditor : Form
	{
		protected Block.BlockType _CurrentType;

		protected Point _MouseLoc;
		protected bool _DrawBlock = false;
		protected bool _Fillblock = false;

		protected List<Block> _Blocks = new List<Block>();
		protected List<Ball> _Balls = new List<Ball>();

		public LevelEditor()
		{
			InitializeComponent();

			this.Button1.Click += new EventHandler(Button1_Click);
			this.Button2.Click += new EventHandler(Button1_Click);
			this.Button3.Click += new EventHandler(Button1_Click);
			this.Button4.Click += new EventHandler(Button1_Click);
			this.Button5.Click += new EventHandler(Button1_Click);
			this.Button6.Click += new EventHandler(Button1_Click);

			this.panel1.MouseDown += new MouseEventHandler(panel1_MouseDown);
			this.panel1.MouseEnter += new EventHandler(panel1_MouseEnter);
			this.panel1.MouseLeave += new EventHandler(panel1_MouseLeave);
			this.panel1.MouseMove += new MouseEventHandler(panel1_MouseMove);
			this.panel1.MouseUp += new MouseEventHandler(panel1_MouseUp);
			this.panel1.Paint += new PaintEventHandler(panel1_Paint);

		}

		void Button1_Click(object sender, EventArgs e)
		{
			try
			{
				_CurrentType = (Block.BlockType)Enum.Parse(typeof(Block.BlockType), ((Button)sender).Text);
			}
			catch (Exception ex)
			{
				
				MessageBox.Show(ex.Message);
			}
		}

		void panel1_Paint(object sender, PaintEventArgs e)
		{
			Color BlockColour = Color.Black ;
			foreach (Block b in _Blocks)
			{
				switch (b.Type)
				{
				case Block.BlockType.Normal:
					BlockColour = Color.Orange;
					break;
				case Block.BlockType.Indestructable:
					BlockColour = Color.Black;
					break;
				case Block.BlockType.Forged:
					BlockColour = Color.Red;
					break;
				case Block.BlockType.Robust:
					BlockColour = Color.Brown;
					break;
				case Block.BlockType.PhaseShifter:
					BlockColour = Color.Blue;
					break;
				case Block.BlockType.Rogue:
					BlockColour = Color.Yellow;
					break;
				default:
					BlockColour = Color.Orange;
					break;
				}
				e.Graphics.FillRectangle(new SolidBrush(BlockColour), new RectangleF(b.Location, new Size(60, 15)));
			}

			if (_DrawBlock)
			{
				switch (_CurrentType)
				{
				case Block.BlockType.Normal:
					BlockColour = Color.Orange;
					break;
				case Block.BlockType.Indestructable:
					BlockColour = Color.Black;
					break;
				case Block.BlockType.Forged:
					BlockColour = Color.Red;
					break;
				case Block.BlockType.Robust:
					BlockColour = Color.Brown;
					break;
				case Block.BlockType.PhaseShifter:
					BlockColour = Color.Blue;
					break;
				case Block.BlockType.Rogue:
					BlockColour = Color.Yellow;
					break;
				default:
					break;
				}

				e.Graphics.DrawRectangle(new Pen(BlockColour), new Rectangle(_MouseLoc, new Size(60, 15)));
				e.Graphics.DrawLine(new Pen(Color.Black), new Point(_MouseLoc.X, 0), new Point(_MouseLoc.X, panel1.Height));
				e.Graphics.DrawLine(new Pen(Color.Black), new Point(_MouseLoc.X+60, 0), new Point(_MouseLoc.X+60, panel1.Height));
				e.Graphics.DrawLine(new Pen(Color.Black), new Point(0, _MouseLoc.Y), new Point(this.panel1.Width, _MouseLoc.Y));
				e.Graphics.DrawLine(new Pen(Color.Black), new Point(0, _MouseLoc.Y + 15), new Point(this.panel1.Width, _MouseLoc.Y + 15));
			}

		}

		void panel1_MouseUp(object sender, MouseEventArgs e)
		{
			_Fillblock = false;
			AddBlock();
		}

		private void AddBlock()
		{
			Block newBlock = new Block(_CurrentType);
			newBlock.Location = _MouseLoc;
			_Blocks.Add(newBlock);
		}

		void panel1_MouseMove(object sender, MouseEventArgs e)
		{
			_MouseLoc = e.Location;
			panel1.Refresh();
		}

		void panel1_MouseLeave(object sender, EventArgs e)
		{
			_DrawBlock = false;
			panel1.Refresh();
		}

		void panel1_MouseEnter(object sender, EventArgs e)
		{
			_DrawBlock = true;
		}

		void panel1_MouseDown(object sender, MouseEventArgs e)
		{
			_Fillblock = true;
		}

		private void exitToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void saveLevelToolStripMenuItem_Click(object sender, EventArgs e)
		{
			if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
			{
				utility.LevelLoader.SaveLevelToFile(new System.IO.FileInfo(saveFileDialog1.FileName), _Blocks, _Balls);
			}
		}

		private void LevelEditor_Load(object sender, EventArgs e)
		{

		}

		private void newLevelToolStripMenuItem_Click(object sender, EventArgs e)
		{
			_Blocks.Clear();
			_Balls.Clear();
			_CurrentType = default(Block.BlockType);
		}
	}
}
